/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FzSamples;
import me.zhengjie.domain.SampleTechnology;
import me.zhengjie.repository.FzSamplesRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.SampleTechnologyRepository;
import me.zhengjie.service.SampleTechnologyService;
import me.zhengjie.service.dto.SampleTechnologyDto;
import me.zhengjie.service.dto.SampleTechnologyQueryCriteria;
import me.zhengjie.service.mapstruct.SampleTechnologyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zcc
* @date 2021-07-05
**/
@Service
@RequiredArgsConstructor
public class SampleTechnologyServiceImpl implements SampleTechnologyService {

    private final SampleTechnologyRepository sampleTechnologyRepository;
    private final SampleTechnologyMapper sampleTechnologyMapper;
    private final FzSamplesRepository fzSamplesRepository;

    @Override
    public Map<String,Object> queryAll(SampleTechnologyQueryCriteria criteria, Pageable pageable){
        Page<SampleTechnology> page = sampleTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sampleTechnologyMapper::toDto));
    }

    @Override
    public List<SampleTechnologyDto> queryAll(SampleTechnologyQueryCriteria criteria){
        return sampleTechnologyMapper.toDto(sampleTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SampleTechnologyDto findById(String techId) {
        SampleTechnology sampleTechnology = sampleTechnologyRepository.findById(techId).orElseGet(SampleTechnology::new);
        ValidationUtil.isNull(sampleTechnology.getTechId(),"SampleTechnology","techId",techId);
        return sampleTechnologyMapper.toDto(sampleTechnology);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SampleTechnologyDto create(SampleTechnology resources) {
        String techid = IdUtil.simpleUUID();
        resources.setTechId(techid);
        SampleTechnology sampleTechnology = sampleTechnologyRepository.save(resources);

        FzSamples samples = fzSamplesRepository.findById(sampleTechnology.getSampleId()).orElseGet(FzSamples::new);
        List<SampleTechnology> technologies = sampleTechnologyRepository.findBySampleId(sampleTechnology.getSampleId());

        samples.setSampleTechnologyId(technologies.stream().map(SampleTechnology::getTechId).collect(Collectors.joining(",")));
        fzSamplesRepository.save(samples);

        return sampleTechnologyMapper.toDto(sampleTechnology);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SampleTechnology resources) {
        SampleTechnology sampleTechnology = sampleTechnologyRepository.findById(resources.getTechId()).orElseGet(SampleTechnology::new);
        ValidationUtil.isNull( sampleTechnology.getTechId(),"SampleTechnology","id",resources.getTechId());
        sampleTechnology.copy(resources);
        sampleTechnologyRepository.save(sampleTechnology);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String techId : ids) {
            SampleTechnology sampleTechnology =  sampleTechnologyRepository.findById(techId).orElseGet(SampleTechnology::new);
            sampleTechnologyRepository.deleteById(techId);

            FzSamples samples = fzSamplesRepository.findById(sampleTechnology.getSampleId()).orElseGet(FzSamples::new);
            List<SampleTechnology> technologies = sampleTechnologyRepository.findBySampleId(sampleTechnology.getSampleId());

            samples.setSampleTechnologyId(technologies.stream().map(SampleTechnology::getTechId).collect(Collectors.joining(",")));
            fzSamplesRepository.save(samples);
        }
    }

    @Override
    public void download(List<SampleTechnologyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SampleTechnologyDto sampleTechnology : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("样品id", sampleTechnology.getSampleId());
            map.put("颜色", sampleTechnology.getColor());
            map.put("1#", sampleTechnology.getTech1());
            map.put("2#", sampleTechnology.getTech2());
            map.put("3#", sampleTechnology.getTech3());
            map.put("4#", sampleTechnology.getTech4());
            map.put("5#", sampleTechnology.getTech5());
            map.put("6#", sampleTechnology.getTech6());
            map.put("7#", sampleTechnology.getTech7());
            map.put("8#", sampleTechnology.getTech8());
            map.put("9#", sampleTechnology.getTech9());
            map.put("10#", sampleTechnology.getTech10());
            map.put("11#", sampleTechnology.getTech11());
            map.put("12#", sampleTechnology.getTech12());
            map.put("13#", sampleTechnology.getTech13());
            map.put("14#", sampleTechnology.getTech14());
            map.put("15#", sampleTechnology.getTech15());
            map.put("16#", sampleTechnology.getTech16());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}